package com.lh.web.controller.shop;

import com.lh.common.entity.admin.*;
import com.lh.common.enums.*;
import com.lh.common.exception.RRException;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import com.lh.web.config.OvmalConfig;
import com.lh.web.controller.AbstractController;
import com.lh.web.service.serviceAdmin.banner.BannerService;
import com.lh.web.service.serviceAdmin.period.PeriodService;
import com.lh.web.service.serviceAdmin.shop.ActivityShopOneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FH
 * @ClassName: ActivityShopOneAction
 * @Description: (活动管理)
 * @date: 2020-05-12 15:49
 */
@Api(value="ActivityShopOneAction",tags={"一元夺宝商品管理"})
@RestController
@RequestMapping("activityShopOne")
public class ActivityShopOneAction extends AbstractController {

    @Autowired
    private ActivityShopOneService activityShopOneService;

    @Autowired
    private PeriodService periodService;

    @Autowired
    private BannerService bannerService;

    @Autowired
    private OvmalConfig ovmalConfig;

    /**
     * 查询一元夺宝商品列表
     */
    @ApiOperation(value = "查询一元夺宝商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "activityName", value = "活动名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "activityStatus", value = "商品状态", required = false, dataTypeClass= ActivityStatusEnum.class)
    })
    @GetMapping("/getActivityShopOneList")
    @RequiresPermissions("activityShopOne:getActivityShopOneList")
    public Result<ActivityShopOne> getActivityShopOneList(String activityName, ActivityStatusEnum activityStatus, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("activityName", activityName);
        if(activityStatus != null) {
            params.put("activityStatus", activityStatus.getValue());
        }
        PageUtils<ActivityShopOne> activityShopOnePage = activityShopOneService.queryActivityShopOneList(params,pageNum,pageSize);
        return Result.ok(activityShopOnePage);
    }

    /**
     * 新增一元夺宝商品
     */
    @ApiOperation(value = "新增/修改一元夺宝商品")
    @PostMapping("/addActivityShopOne")
    @RequiresPermissions("activityShopOne:addActivityShopOne")
    public Result<ActivityShopOne> addActivityShopOne(@RequestBody ActivityShopOne activityShop) {
        AssertUtil.assertNotTrue(activityShop.getShopPrice().equals(BigDecimal.ZERO),Constant.RESULT.FI1000, "商品价格不能为ZERO!");
        AssertUtil.assertNotTrue(activityShop.getShopSingleprice().equals(BigDecimal.ZERO),Constant.RESULT.FI1000, "商品成品价不能为ZERO!");
        AssertUtil.assertNotTrue(activityShop.getProfitAmount().equals(BigDecimal.ZERO),Constant.RESULT.FI1000, "盈利金额不能为ZERO!");
        AssertUtil.assertNotTrue((activityShop.getProfitAmount().compareTo(activityShop.getShopPrice())<0),Constant.RESULT.FI1000, "盈利金额小于商品价格!");
        try {
            if(activityShop.getAid() == null) {
                activityShopOneService.insertActivityShopOne(activityShop);
                return Result.ok(activityShop);
            }
            AssertUtil.assertNotNull(activityShop.getAid(), Constant.RESULT.FI1000, "活动商品ID为空!");
            activityShopOneService.updateActivityShopOne(activityShop);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("新增/修改一元夺宝商品异常!");
        }
    }

    /**
     * 活动关联商品查询
     */
    @ApiOperation(value = "活动关联商品查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "shopName", value = "商品名称", required = false, dataTypeClass= String.class)
    })
    @GetMapping("/getActivityShopList")
    @RequiresPermissions("shop:getActivityShopList")
    public Result<BannerShop> getActivityShopList(String shopName, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("shopName", shopName);
        PageUtils<BannerShop> shopMyPage = activityShopOneService.getActivityShopList(params,pageNum,pageSize);
        return Result.ok(shopMyPage);
    }

    /**
     * 一元夺宝详情页面
     */
    @ApiOperation(value = "一元夺宝详情页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "aid", value = "一元夺宝活动ID", required = true, dataTypeClass= Long.class),
            @ApiImplicitParam(paramType="query", name = "period", value = "商品期数", required = true, dataTypeClass= Long.class)
    })
    @GetMapping("/getShopOneDetails")
    @RequiresPermissions("activityShopOne:getShopOneDetails")
    public Result<ActivityShopDetails> getShopOneDetails(Long aid,Long period){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("aid",aid);
        params.put("period",period);
        ActivityShopDetails activityShopOne = activityShopOneService.queryShopOneDetails(params);
        return Result.ok(activityShopOne == null ? new ActivityShopDetails():activityShopOne);

    }

    /**
     * 编辑查看数据
     */
    @ApiOperation(value = "编辑查看数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "aid", value = "一元夺宝活动ID", required = true, dataTypeClass= Long.class)
    })
    @GetMapping("/getShopOne")
    @RequiresPermissions("activityShopOne:getShopOne")
    public Result<ActivityShopDetails> getShopOne(Long aid){
        AssertUtil.assertNotNull(aid, Constant.RESULT.FI1000, "活动商品ID为空!");
        ActivityShopOne activityShopOne = activityShopOneService.queryShopOne(aid);
        return Result.ok(activityShopOne == null ? new ActivityShopDetails():activityShopOne);

    }

    @ApiOperation(value = "一元夺宝上下架/删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "aid", value = "aid", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "status", value = "状态", required = false, dataTypeClass = UpperLowerShelfStatus.class),
            @ApiImplicitParam(paramType="query",name = "isDel", value = "删除", required = false, dataTypeClass = IsDel.class)
    })
    @ResponseBody
    @GetMapping("/updateActivityShopOneStatus")
    @RequiresPermissions("activityShopOne:updateActivityShopOneStatus")
    public Result updateActivityShopOneStatus(Long aid,UpperLowerShelfStatus status,IsDel isDel) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("aid",aid);
        Period period = periodService.queryPeriod(params);
        if(status != null) {
            params.put("status",status.getValue());
            if(status.equals(UpperLowerShelfStatus.UPPERSHELF)){
                AssertUtil.assertObjectEmpty(period, Constant.RESULT.FI1000, "该商品期数未结束不能上架!");
                int i = activityShopOneService.queryCountShopOne();
                if(i >= ovmalConfig.getCount()){
                    return Result.error("活动上架状态最多"+ovmalConfig.getCount()+"个，超过不可再上架新活动");
                }
                ActivityShopOne activityShopOne = activityShopOneService.queryShopOne(aid);
                LotteryTypeStatus activityType = activityShopOne.getActivityType();
                if(activityType.equals(LotteryTypeStatus.STARTTIME)){
                    params.put("activityType",activityType.getValue());
                    Date activityStartTime = activityShopOne.getActivityStartTime();
                    if(activityStartTime.getTime()>new Date().getTime()){
                        params.put("activityStartTime",activityStartTime);
                    }else {
                        params.put("activityStartTime",new Date(new Date().getTime()+10000));
                    }
                }else {
                    params.put("activityType",activityType.getValue());
                }
            }else {
                int i1 = bannerService.queryBanner(aid);
                if(i1 > 0){
                    return Result.error("该活动被上架Banner关联,不能下架!");
                }
            }
        }
        if(isDel != null){
            params.put("isDel",isDel.getValue());
        }
        activityShopOneService.updateActivityShopOneStatus(params);
        return Result.ok();
    }

    /**
     * 获取当前系统时间
     */
    @ApiOperation(value = "获取当前系统时间")
    @GetMapping("/getDate")
    public Result getDate() {
        return Result.ok(new Date());
    }
}
